var pos = {}
var data = {}
function posLogin(){
	pos.uniqueId = $("posUniqueId").value
	DWR.call("pospService.login",pos.uniqueId,function(r){
		pos.uniqueId = r.posNo
		pos.sn = r.tradeId
		
		$("pos_login_img").src = "/images/table/drop-yes.gif"
		alert("pospService.login ok")
	})
}


//操作员签到
function signIn(){
	var uniqueId = $("operatorUniqueId").value
	DWR.call("pospService.signIn",pos.uniqueId,uniqueId,$("operpassword").value,function(r){
		cOper.id = r.operId
		$("sign_in_img").src = "/images/table/drop-yes.gif"
		alert("pospService.signIn ok")
	})
} 

function showData(d){
	if (!d) d = {}
	d.balance1 = Format.fenToYuan(d.balance1)
	d.balance2 = Format.fenToYuan(d.balance2)
	Common.setValues("dataform",d)
}
//操作员签退
function signOut(){
	DWR.call("pospService.signOut",pos.uniqueId,cOper.id,function(){
		showData({})
		$("sign_in_img").src = "/images/table/drop-no.gif"
		alert("ospService.signOut ok")
	})
} 
//终端解锁
function posUnLock(){
	DWR.call("pospService.posUnLock",pos.uniqueId,cOper.id,$("posUnLockPassword").value,function(){
		alert("pospService.posUnLock ok")
	})
}

//操作员改密
function changePassword(){
	DWR.call("pospService.modifyPassword",pos.uniqueId,cOper.id,$("newpassword").value,$("oldpassword").value,function(){
		alert("pospService.modifyPassword ok")
	})
}
//增加汇率
function saveExchangeRate(){
	var er = {
		rate:$("exchangeRate").value * 10000,
		currency:Currency.PATACA
	}
	DWR.call("exchangeRateService.saveExchangeRate",er,function(v){
		alert("exchangeRateService.saveExchangeRate ok")
		DWR.call('exchangeRateService.startExchangeRate',v.id,function(r){
			alert("汇率启用成功!")
		});
	});
}
//卡相关
var card = {
	expiredTime : "20081101"
}
//取得卡片数据
function getCardData(){
	var currency;
	if ($("currency").value == Currency.RMB)
		currency = "Y"
	else if ($("currency").value == Currency.PATACA)
		currency = "N"
	else{
		showError("不支持的币种!");return;
	}
	var zhgjCardType = $("zhgjcardtype").value
	if (!zhgjCardType)
		zhgjCardType = "00";
	var et = $("expiredTime").value
	if (et){
		card.expiredTime = et.replaceAll("-","")
	}
	return zhgjCardType + card.expiredTime+ currency +"9010"
}
//发卡zhgj
function sellCard(){
	var asn = $("asn").value
	DWR.call("pospService.sellCard",pos.uniqueId,cOper.id,asn,getCardData(),function(v){
		alert("pospService.sellCard ok")
		showData(v);
	});
}
//充值zhgj
function cardDeposit(){
	try{
		var amount = $("amount").value
		var asn = $("asn").value
		pos.sn ++
		var data = {}
		data.tradeName = "充值"
		data.balance1 = getCardBalance()
		var result = DWR.syncCall("pospService.deposit",pos.uniqueId,cOper.id,pos.sn,asn,Format.yuanToFen(amount),getCardData(),data.balance1);
		data.balance2 = getCardBalance()
		showData(Card.merge(data,result));
		alert("pospService.deposit ok")
	}catch(e){showError(e.description)}
}
function getCardBalance(v){
	var asn = $("asn").value
	return DWR.syncCall("zhgjService.testGetBalance",asn)
}

//充值冲正
function reversal(){
	try{
		var amount = $("reversalAmount").value
		var asn = $("asn").value
		var cardData = getCardData()
		pos.sn ++
		var data = {}
		data.tradeName = "充值冲正-充值"
		data.balance1 = getCardBalance()
		var result = DWR.syncCall("pospService.deposit",pos.uniqueId,cOper.id,pos.sn,asn,Format.yuanToFen(amount),cardData,data.balance1)
		data.balance2 = getCardBalance();
		showData(Card.merge(data,result));
		alert("pospService.deposit ok")
		
		data.tradeName = "充值冲正-冲正"
		var res = DWR.syncCall("pospService.rollbackLoad",pos.uniqueId,cOper.id,pos.sn)
		data.balance2 = getCardBalance();
		showData(data);
		alert("pospService.rollbackLoad ok")
	}catch(e){showError(e.description)}
}
//充值撤销
function cancelTrade(){
	try{
		var amount = $("amount").value
		var asn = $("asn").value
		var tradeId = $("tradeId").value
		pos.sn ++
		var data = {}
		data.tradeName = "充值撤销-临时"
		data.balance1 = getCardBalance();
		DWR.syncCall("pospService.cancelCardDeposit",pos.uniqueId,cOper.id,pos.sn,asn,amount*100,data.balance1,tradeId);
		data.balance2 = getCardBalance();
		showData(data);
		alert("pospService.cancelCardDeposit ok")
		data.tradeName = "充值撤销-确认"
		DWR.syncCall("pospService.confirmUnload",pos.uniqueId,cOper.id,pos.sn);
		data.balance2 = getCardBalance();
		showData(data);
		alert("pospService.confirmUnload ok")
	}catch(e){showError(e.description)}
}
//年审
function annualAudit(){
	try{
		var res = DWR.syncCall("pospService.annualAudit",pos.uniqueId,cOper.id,$("asn").value,getCardData())
		card.expiredTime = res.expiredTime
		$("expiredTime").value = res.expiredTime.substring(0,4)+"-"+res.expiredTime.substring(4,6)+"-"+res.expiredTime.substring(6,8)
		alert("pospService.annualAudit ok " + $("expiredTime").value)
	}catch(e){showError(e.description)}
}
//退卡
function retCard(){
	try{
		var asn = $("asn").value
		pos.sn ++
		var data = {}
		data.tradeName = "退卡"
		var balance = getCardBalance()
		data.balance1 = balance
		var res = DWR.syncCall("pospService.cashWithdraw",pos.uniqueId,cOper.id,pos.sn,asn,balance,balance,getCardData())
		data.balance2 = getCardBalance()
		showData(data)
		alert("pospService.cashWithdraw ok "+res.msg)
		res = DWR.syncCall("pospService.retCard",pos.uniqueId,cOper.id,pos.sn,asn);
		alert("pospService.retCard ok " + Common.getText("card_status",res.status))
	}catch(e){showError(e.description)}
}
//是否黑名单卡
function checkBlackCard(){
	var asn = $("asn").value
	DWR.call("zhgjService.checkBlackCard",asn,function(){
		alert("zhgjService.checkBlackCard ok ")
	})
}
//对账
function settle(){
	try{
		var res = DWR.syncCall("pospService.settle",pos.uniqueId,cOper.id)
		alert("pospService.settle ok")
	}catch(e){showError(e.description)}
}
//连安控
function connectHSPos(){
	DWR.call("pospService.getSectorKey",pos.uniqueId,"1111",function(){
		alert("pospService.getSectorKey ok ")
	})
}

//导交易
function transferTx(){
	DWR.call("zhgjService.transferTx",function(){
		alert("zhgjService.transferTx ok ")
	})
}
function transferMgnt(){
	DWR.call("zhgjService.transferMgnt",function(){
		alert("zhgjService.transferMgnt ok ")
	})
}
function autoTransfer(){
	DWR.call("zhgjService.autoTransfer",function(){
		alert("zhgjService.autoTransfer ok ")
	})
}


//充值zhgj
function cardPurchase(){
	try{
		var amount = $("purchaseAmount").value
		var asn = $("asn").value;
		var giftId = $("giftSelect").value;
		var discount = $("discount").value;
		var cardTxNo = "1234";
		var ds = new Date().format("yyyyMMddhhmmss");
		pos.sn ++
		var data = {}
		data.tradeName = "消费"
		data.balance1 = getCardBalance()
		
		var result = DWR.syncCall("pospService.purchase",pos.uniqueId,cOper.id,pos.sn,asn,Format.yuanToFen(amount),getCardData(),data.balance1,giftId,Format.yuanToFen(discount),cardTxNo,ds);
		data.balance2 = getCardBalance()
		showData(Card.merge(data,result));
		alert("pospService.purchase ok")
	}catch(e){showError(e.description)}
}
//备用金余额查询
function querySpareBalance(){
	try{
		var asn = $("asn").value
		var balance1 = getCardBalance()
		var result = DWR.syncCall("pospService.querySpareBalance",pos.uniqueId,cOper.id,asn,balance1,getCardData());
		$("spareBalance").value = Format.fenToYuan(result.balance)
		alert("pospService.querySpareBalance ok")
	}catch(e){showError(e.description)}
}
//圈存
function cardLoad(){
	try{
		var amount = $("loadAmount").value
		var asn = $("asn").value
		pos.sn ++
		var data = {}
		data.tradeName = "圈存"
		data.balance1 = getCardBalance()
		var result = DWR.syncCall("pospService.load",pos.uniqueId,cOper.id,pos.sn,asn,Format.yuanToFen(amount),getCardData(),data.balance1);
		data.balance2 = getCardBalance()
		showData(Card.merge(data,result));
		alert("pospService.load ok")
	}catch(e){showError(e.description)}
}

function importOldCard(){
	try{
		var result = DWR.syncCall("testZhgjService.importOldSystemCardData");
		alert("OK")
	}catch(e){showError(e.description)}
}

function getCardReport(){
	try{
		var result = DWR.syncCall("pospService.getCardReport",pos.uniqueId);
		alert("OK  " + result.amount)
	}catch(e){showError(e.description)}
}